from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(options, service)

link = "https://www.jd.com"

driver.get(link)
time.sleep(5)

driver.save_screenshot('123.png')

try:
    # 模拟人滚动滚动条,处理图片懒加载问题
    js_height = "return document.body.clientHeight"
    driver.get(link)
    k = 1
    height = driver.execute_script(js_height)
    #while True:
    if k * 500 < height:
        js_move = "window.scrollTo(0,{})".format(k * 500)
        print(js_move)
        driver.execute_script(js_move)
        time.sleep(0.2)
        height = driver.execute_script(js_height)
        k += 1
        #else:
        #    break

    time.sleep(1)

    # 7>  # 直接截图截不全，调取最大网页截图
    width = driver.execute_script(
        "return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
    height = driver.execute_script(
        "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
    print(width, height)
    driver.set_window_size(width + 100, height + 100)
    time.sleep(1)
    driver.save_screenshot('123.png')

except Exception as e:
        print(e)


element_list = driver.find_elements(By.XPATH,'//*[@id="feedContent0"]/li/a')

for element in element_list:
     print(element.text)
     print(element.get_attribute('href'))

#print(driver.find_elements(By.CSS_SELECTOR,'li.more2_item')[0].text)

#driver.get_screenshot_as_file('123.png')

#with open('test.html','w') as f:
#    f.write(driver.page_source)

#driver.close()